package com.api.shopapi.mapper;

import com.api.shopapi.entity.MedicationScheduling;
import com.api.shopapi.entity.dto.MedicationSchedulingDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface MedicationSchedulingMapper {

    @Insert("insert into medication_scheduling " +
            "(MedicationSchedulingCode,Day,Time,CreateTime,Status,StaffCode,InstitutionCode,ElderlyCode) " +
            "values (#{medicationScheduling.MedicationSchedulingCode},#{medicationScheduling.Day},#{medicationScheduling.Time}," +
            "#{medicationScheduling.CreateTime} ,#{medicationScheduling.Status} ,#{medicationScheduling.StaffCode},#{medicationScheduling.InstitutionCode},#{medicationScheduling.ElderlyCode} )")
    int insert(@Param("medicationScheduling") MedicationScheduling medicationScheduling);

    @Update("update medication_scheduling set Day =#{medicationScheduling.Day},Time =#{medicationScheduling.Time},CreateTime=#{medicationScheduling.CreateTime}," +
            "Status=#{medicationScheduling.Status} ,StaffCode=#{medicationScheduling.StaffCode},InstitutionCode=#{medicationScheduling.InstitutionCode},ElderlyCode=#{medicationScheduling.ElderlyCode} " +
            "where medicationSchedulingCode=#{medicationScheduling.medicationSchedulingCode}  ")
    int updateByMedicationSchedulingCode(@Param("medicationScheduling") MedicationScheduling medicationScheduling);

    @Delete("delete from medication_scheduling where medicationSchedulingCode=#{medicationSchedulingCode} ")
    int deleteByMedicationSchedulingCode(@Param("medicationSchedulingCode") String medicationSchedulingCode);

    @Select("select * from medication_scheduling where medicationSchedulingCode=#{medicationSchedulingCode} limit 1")
    MedicationScheduling getByMedicationSchedulingCode(@Param("medicationSchedulingCode") String medicationSchedulingCode);

    @Select("select * from medication_scheduling where Time=#{time} and ElderlyCode=#{elderlyCode} limit 1")
    MedicationScheduling getByTimeAndElderlyCode(@Param("time") Date time,@Param("elderlyCode") String elderlyCode);

    @Select("<script>" +
            "select m.*,u.Name ElderlyName from medication_scheduling m,elderly e,user u " +
            "where m.InstitutionCode = #{institutionCode} and m.ElderlyCode=e.ElderlyCode and e.UserCode=u.UserCode " +
            "and (m.Time between #{startTime} and #{endTime}) " +
            "<if test ='elderlyName !=null and elderlyName!=\"\" '>" +
            "and u.Name like concat('%',#{elderlyName},'%') " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and m.Status =#{status} " +
            "</if>" +
            " order by Time asc" +
            "</script>")
    List<MedicationSchedulingDTO> getPageList(@Param("institutionCode")String institutionCode,@Param("elderlyName") String elderlyName,
                                              @Param("status") int status, @Param("startTime")String startTime, @Param("endTime")String endTime);


    @Select("<script>" +
            "select * from medication_scheduling" +
            " where InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} ) " +
            " order by Time asc" +
            "</script>")
    List<MedicationSchedulingDTO> listByGovCode(@Param("governmentCode")String governmentCode);


}
